Understanding Important Concepts
To best explain this architecture and my findings, it is important for the uninitiated developer to first understand a number of background concepts about Java, J2EE, and some programming architectural concepts. If you are already familiar with Java, J2EE, EJB components, and the Model View Controller (MVC) architecture, go directly to the next section, Analyzing the J2EE Application.
A Java Primer. To start with, Java is an object-oriented
programming language. What this means—in a tiny nutshell—is
that the primary focus for Java is manipulating objects. For example,
String is a Java object, and as an object, it has certain
inherent properties; it stores alphanumeric data, is a member of the
java.lang package, and has a number of methods. You use
these methods to perform specific actions—much like functions
in ColdFusion. For example, the toUpperCase() method converts
all of the characters in a String object into upper case.
However, unlike ColdFusion functions, you must create an instance of
the String object before you can perform any methods on
it. So, for example, you would first create a variable called myString
that was an instance of the String object. Once you create
it, you could then call the method after the variable name: myString.toUpperCase().
Java programming is essentially a process of creating and using objects
and methods.
There are a number of other Java aspects that make it not only powerful,
but also more difficult to program in. For example, Java is strong
typed. This means that if you create a variable of data type
int, you can only store integer numbers in it—setting
it to anything else will cause an error. Java is low level,
which means that you can control everything from how your application
interacts with the CPU and memory to network operations and more. Java
uses of classes, which are essentially compiled Java
source files that contain a number of objects. You can create a number
of different types of classes—and J2EE has class types that are
different from regular Java. This is quite a different world from ColdFusion,
which is loosely typed. ColdFusion automatically figures out a variable’s
data type. It is script-based, which means you can use functions whenever
you want; you don’t have to create or use objects as you must
in the traditional object-oriented programming method.
Java 2 Enterprise Edition (J2EE). This leads into what J2EE is—and isn’t. J2EE is a standard, it is not a programming language. J2EE is an implementation of the Java programming language, but includes a number of Application Programming Interfaces (APIs) for connecting to databases, queuing messages, connecting to registries, and a set of application elements such as JSP, servlets and EJB—all of these APIs are important when developing web or enterprise-level applications. A number of different software vendors offer J2EE-compliant application servers. These are simply implementations of the J2EE standard. Some examples include Macromedia JRun, IBM WebSphere, BEA WebLogic and Sun iPlanet Application Servers. To develop in J2EE, you need to use a J2EE application server.
Application servers started out as servlet engines. They handled the deployment of servlets, which were Java classes developed to perform processes and return web pages. Developing servlets in the early days of web development was cumbersome. You had to hard code HTML directly into output methods and there was no way to differentiate between the coding logic and the presentation logic. JSP arose to deal with this problem. With JSP, developers used a simpler tag-based scripting language mixed with HTML to produce dynamic information to web pages—similar to CFML. The JSP container would parse the complete JSP template into a servlet, which it could then execute. JSP pages can interact with servlets and other Java classes, somewhat separating the logic and display while at the same time maintaining the low-level power of Java.
Two other technologies are crucial to many of today’s J2EE applications:
JavaBeans components and EJB components. Despite the similar names,
these two component types bear little resemblance to one another. JavaBeans
components are Java classes that JSP pages or servlets call and use
primarily to store data, such as session information or perform simple
logic processing. When you store information in JavaBeans components,
you can set them using the set() methods (for example,
etUsserName(“jtRipper”)) and you can retrieve
them with get() methods (for example, getUserName()).
Enterprise JavaBeans. EJBs, on the other hand, are deployed components. What this means is that you control EJBs through a container, similar to servlets and JSP pages. The container can handle a number of different factors; the most important one being the ability for you to access EJB components (often called beans) remotely. Whereas you must access JavaBeans or other Java classes from the same machine, you can call an EJB component from external web application servers, Java applets, or even Java-based desktop applications.
There are four primary EJB component types that J2EE developers use:
- Entity beans. Developers use entity beans for data persistence. This means that you can create an entity bean to represent one row of a database. You can retrieve or edit the information directly from the entity bean and the bean will update the database, handle transactions, and roll back changes if an error occurs. In this application, I use the Orders entity bean to interact with the orders table in the database.
- Stateful session beans. Developers used stateful sessions beans to contain session-specific data with applications similar to the way that servlets or ColdFusion pages hold session information with browsers. It’s possible to offset session management in applications entirely to these beans; this could benefit a heterogeneous environment, where a user moves from application-to-application (such as a portal), allowing users to bounce between ColdFusion MX and a number of Java applications, web-based, or otherwise.
- Stateless session beans. Developers generally use stateless session beans to perform business logic. You use these in your app where you need to execute algorithms and other processes or to contain data that doesn’t require session handling.
- Message beans. Developers use message beans specifically for messaging processes in conjunction with JMS.
Together, all of these J2EE elements provide a wide palette of tools for the Java developer. Often, this toolset can be overwhelming. The Java community still debates whether or not developers should use EJB components for web applications and debates best practices for designing J2EE applications, based on performance, scalability, ease-of-use and other criteria. An entire catalog of J2EE patterns, or architecture best practices have emerged to help developers with this process; but unless you’re a seasoned and experienced J2EE architect, this can be a daunting task. Understanding not only these tools and best practices, but also having a solid understanding of object-oriented Java development is the main reason why J2EE architects demand high salaries—there is a lot to know, and it continues to evolve.
The Model View Controller (MVC) Architecture. With all of these development tools available in the J2EE standard, many architects have adopted the Model View Controller (MVC) application design methodology. In this paradigm, an application has three primary aspects:
- The data model. This holds application-specific data, can include session and application scopes, but also handles manipulating data (such as business logic). In J2EE, the model is generally handled by JavaBeans and EJB components.
- The controller. This controls an application’s input and output. With web-based applications, inputs and outputs are usually the HTTP request and response; one or more controller servlets typically handle these inputs and outputs. These also control security, data model loading, and other similar functions.
- The view. This is essentially the user interface (UI). In J2EE, JSP pages best handle the UI.
Submit feedback on our tutorials, articles, and sample applications.